using System.Collections.Generic;

using Haushaltsbuch.DataAcces.Entities;
using Haushaltsbuch.Helpers;

namespace Haushaltsbuch.DataAcces.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Haushaltsbuch.DataAcces.HaushaltsbuchContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            AutomaticMigrationDataLossAllowed = false;
        }

        protected override void Seed(Haushaltsbuch.DataAcces.HaushaltsbuchContext context)
        {
            context.Roles.AddOrUpdate(r => r.Name, 
                new Role(){Name = "Reader"},
                new Role(){Name = "Contributor"},
                new Role(){Name = "Admin"}
                );

            context.SaveChanges();

            var readerRole = context.Roles.Single(r => r.Name == "Reader");
            var contributorRole = context.Roles.Single(r => r.Name == "Contributor");
            var adminRole = context.Roles.Single(r => r.Name == "Admin");

            context.Users.AddOrUpdate(u => u.Username,
                new User(){Username = "Reader", Passwort = PasswordHelper.GetHash("test1"), Firstname = "Lutz", Lastname = "Reader", Roles = new HashSet<Role>{readerRole}},
                new User(){Username = "Contributor", Passwort = PasswordHelper.GetHash("test1"), Firstname = "Jule", Lastname = "Conti", Roles = new HashSet<Role>{contributorRole}},
                new User(){Username = "Admin", Passwort = PasswordHelper.GetHash("test1"), Firstname = "Bernd", Lastname = "Admin", Roles = new HashSet<Role>{adminRole}}
                );

        }
    }
}
